package com.pega.tulokhorecord.util;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.provider.Settings.Secure;
import android.util.Log;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Toast;

import com.pega.tulokhorecord.R;

public class Utils {
	private static Utils instance = null;

	private ProgressDialog progressSpinnerDialog;

	private PackageManager pm;

	private int progressSpinnerDialogCount;

	public Utils() {
		progressSpinnerDialogCount = 0;
	}

	public static void init() {
		if (instance == null) {
			instance = new Utils();
		}
	}

	public static Utils sharedInstance() {
		if (instance == null) {
			instance = new Utils();
		}
		return instance;
	}

	/**
	 * show a dialog
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param icon
	 * @param negButName
	 * @param negButListener
	 * @param posButName
	 * @param posButListener
	 */
	public void showDialog(Context context, int title, int message, int icon,
			int negButName, OnClickListener negButListener, int posButName,
			OnClickListener posButListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);

		builder.setTitle(title).setMessage(message)
				.setNegativeButton(negButName, negButListener)
				.setPositiveButton(posButName, posButListener).setIcon(icon);
		builder.create().show();
	}

	public void showDialog1(Context context, String title, String message,
			int icon, String posButName, OnClickListener posButListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);

		builder.setTitle(title).setMessage(message)
				.setPositiveButton(posButName, posButListener).setIcon(icon);
		builder.create().show();

	}

	/**
	 * show a dialog
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param icon
	 * @param negButName
	 * @param negButListener
	 * @param posButName
	 * @param posButListener
	 */
	public void showDialog(Context context, String title, String message,
			int icon, String negButName, OnClickListener negButListener,
			String posButName, OnClickListener posButListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);

		builder.setTitle(title).setMessage(message)
				.setNegativeButton(negButName, negButListener)
				.setPositiveButton(posButName, posButListener).setIcon(icon);
		builder.create().show();

	}

	/**
	 * show a progress spinner dialog when run another thread
	 * 
	 * @param context
	 * @param title
	 * @param message
	 */
	public void showProgressSpinnerDialog(Context context, String title,
			String message) {
		if (progressSpinnerDialogCount == 0) {
			progressSpinnerDialog = new ProgressDialog(context,
					ProgressDialog.STYLE_SPINNER);
			progressSpinnerDialog.setTitle(title);
			progressSpinnerDialog.setMessage(message);
			progressSpinnerDialog.show();
		}
		progressSpinnerDialogCount += 1;
	}

	/**
	 * dismiss above progress spinner dialog
	 */
	public void dismissProgressSpinnerDialog() {
		progressSpinnerDialogCount -= 1;
		if (progressSpinnerDialogCount == 0) {
			if (progressSpinnerDialog != null) {
				if (progressSpinnerDialog.isShowing())
					progressSpinnerDialog.dismiss();
				progressSpinnerDialog = null;
			}
		}
	}

	/**
	 * show a Toast in Length Long
	 * 
	 * @param context
	 * @param message
	 */
	public void showToast(Context context, String message) {
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}

	/**
	 * log if debugging
	 * 
	 * @param context
	 * @param message
	 */
	public void log(Context context, String message) {
		if (Defines.DEBUG && message != null)
			Log.d(context.getString(R.string.app_name), message);
	}

	/**
	 * Start system SMS to send a SMS with given message.
	 * 
	 * @param context
	 * @param message
	 */
	public void sendSMS(Context context, String message) {
		Intent sendIntent = new Intent(Intent.ACTION_VIEW);
		sendIntent.putExtra("sms_body", message);
		sendIntent.setType("vnd.android-dir/mms-sms");
		context.startActivity(sendIntent);
	}

	/**
	 * Remove accent in Vietnamese character
	 * 
	 * @param strInput
	 * @return
	 */
	public String removeAccent(String strInput) {
		for (int i = 1; i < Defines.VietNamChar.length; i++) {
			for (int j = 0; j < Defines.VietNamChar[i].length(); j++) {
				strInput = strInput.replace(Defines.VietNamChar[i].charAt(j),
						Defines.VietNamChar[0].charAt(i - 1));
			}
		}
		return strInput;
	}

	/**
	 * check does device support FEATURE_TELEPHONY
	 * 
	 * @param context
	 * @return
	 */
	public Boolean checkSupportFEATURE_TELEPHONY(Context context) {
		pm = context.getPackageManager();
		return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
	}

	/**
	 * get location of a view was located on screen
	 * 
	 * @param v
	 * @return Point
	 */
	public Point getLocationOfView(View v) {
		int[] location = new int[2];
		v.getLocationOnScreen(location);
		Point p = new Point();
		p.x = location[0];
		p.y = location[1];
		return p;
	}

	/**
	 * set always show Action Overflow if device has hardware option key
	 * 
	 * @param context
	 */
	public void setAlwaysShowActionOverflow(Context context) {
		try {
			ViewConfiguration config = ViewConfiguration.get(context);
			java.lang.reflect.Field menuKeyField = ViewConfiguration.class
					.getDeclaredField("sHasPermanentMenuKey");
			if (menuKeyField != null) {
				menuKeyField.setAccessible(true);
				menuKeyField.setBoolean(config, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getDeviceID(Context context) {
		return Secure
				.getString(context.getContentResolver(), Secure.ANDROID_ID);
	}

	public boolean isValidEmail(CharSequence target) {
		if (target == null) {
			return false;
		} else {
			return android.util.Patterns.EMAIL_ADDRESS.matcher(target)
					.matches();
		}
	}

	public boolean isValidPassword(CharSequence target) {
		if (target == null) {
			return false;
		} else {

			return target.length() >= 6 && haveLetter(target)
					&& haveNumber(target);
		}
	}

	public boolean haveNumber(CharSequence target) {
		int length = target.length();
		for (int i = 0; i < length; i++) {
			if (Character.isDigit(target.charAt(i))) {
				return true;
			}
		}
		return false;
	}

	public boolean haveLetter(CharSequence target) {
		int length = target.length();
		for (int i = 0; i < length; i++) {
			if (Character.isLetter(target.charAt(i))) {
				return true;
			}
		}
		return false;
	}

	public static int getNumberDigits(String inString) {
		if (isEmpty(inString)) {
			return 0;
		}
		int numDigits = 0;
		int length = inString.length();
		for (int i = 0; i < length; i++) {
			if (Character.isDigit(inString.charAt(i))) {
				numDigits++;
			}
		}
		return numDigits;
	}

	public static boolean isEmpty(String inString) {
		return inString == null || inString.length() == 0;
	}

}
